/**
 * 
 */

NotificationBriefPortlet = Class.extend(
		{
			init : function() {
				this.name = "NotificationBriefPortlet";
				this.intervalID = 0;
			},
			
			onBegin: function()	{
				this.registerObserver();
			},
			
			onNotifBriefButtonClick: function()	{
				var subject = SingletonFactory.getInstance(Subject);
				var request = new Request("NotificationFull",undefined,null); 
				subject.notifyEvent('RequestRoute', request); 
			},

			setInterval : function() {
				this.intervalID = setInterval(this.doAjax, 1000);
			},

			doAjax : function() {
				var app = SingletonFactory.getInstance(Application);
				var root = app.getSystemProperties().get("host.root");
				var currentNotifID = app.getSystemProperties().get("notification.maxNotiID");
				if(currentNotifID === undefined){
					currentNotifID = 0;
				}

				// get notification
				// FIXME: get userid here
				$.get(root + "ajax/get-live-notification/", {},
						function(ret) {
							// {"id":"2","result":{"1":{"id":"1","name":"B\u00e9
							// Heo"},"5":{"id":"2","name":"Minhthedt
							// Thedaica"}}}
							ret = $.parseJSON(ret);
							ret = ret.result;
							if (currentNotifID == ret.id){
								return;
							}
							
							if(ret.num == 0) {
								//clear and return
								var subject = SingletonFactory.getInstance(Subject);
								subject.notifyEvent("NotificateClear", ret);
								return;
							}
							var currentNotifID = ret.num;
							app.getSystemProperties().set("notification.maxNotiID", currentNotifID);
							ret = ret.result;
							var subject = SingletonFactory.getInstance(Subject);
							subject.notifyEvent("NotificateAfterAjax", ret);
						});
			},
			
			onNotificateClear: function (){
//				this.requestForResource("notification-live").empty().hide();
			},

			onNotificateAfterAjax : function(ret) {
				var app = SingletonFactory.getInstance(Application);
				var currentNotifID = app.getSystemProperties().get("notification.maxNotiID");
				var liveNum = this.getLocalizedMessage('LiveNotification', currentNotifID);
				$("#NotificationBriefPortlet-notification-live").empty().append(liveNum).show().css("display","inline-block").css("color","red");
				
				// Định notify cho đẹp, ai dè teo
//				for ( var key in ret) {
//					var profilename = ret[key]['name'];
//					this.notification(profilename," has sent you an notification");
//				}
			},

			notification : function(from, message) {
				// initialize notification area
//				//console.log($(".ui-notification-container"));
//				$(".ui-notification-container").notification();

//				  $(".ui-notification-container").notification("create", {
//			            title: "Hello world!",
//			            content: "I am a default notification"
//			        });
			},

			run : function() {
				this.doAjax();
				this.setInterval();
				this.getPortletPlaceholder().drawToCanvas(this.render());
			},
			
			onEnd: function(){
				clearInterval(this.intervalID);
				this.unregisterObserver();
			}
		}).implement(RenderInterface).implement(PortletInterface).implement(ObserverInterface);

ViewFullNotificationHandler = Handler.extend({
	getEventName: function()	{
		return "click";
	},
	
	onEvent: function(){
		alert("donganh");
	}
});